package com.woniuxy.woniuhis.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.woniuhis.entity.Medicine;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.mapper.MedicineMapper;
import com.woniuxy.woniuhis.service.MedicineService;

@Service
public class MedicineServiceImpl implements MedicineService{

	@Autowired
	MedicineMapper medicineMapper;
	
	
	/* 
	 * 找到所有药物的服务方法
	 */
	@Override
	public List<Medicine> findAllMedicine() {
		
		try {
			
			List<Medicine> medis = medicineMapper.selectAllMedicine();
			return medis;
			
		} catch (Exception e) {
			
			System.out.println("findAllMedicine错误");
			e.printStackTrace();
			throw new MedicineServiceException("findAllMedicine错误");
			
		}
	
	}


	@Override
	public List<Medicine> findMedicineByName(Medicine medicine) {
		
		try {
			
			List<Medicine> medis = medicineMapper.selectMediByMediName(medicine);
			return medis;
			
		} catch (Exception e) {
			
			System.out.println("findMedicineByName错误");
			e.printStackTrace();
			throw new MedicineServiceException("findMedicineByName错误");
			
		}
	}


	/* 
	 * 添加一个药物
	 */
	@Override
	public void addMedicine(Medicine medicine) {
		try {
			
			System.out.println("添加");
			medicine.setMediId(UUID.randomUUID().toString());
			medicineMapper.insertMedi(medicine);
			
		} catch (Exception e) {
			
			System.out.println("addMedicine错误");
			e.printStackTrace();
			throw new MedicineServiceException("addMedicine错误");
			
		}
		
	}


	/* 
	 * 修改药物的服务方法
	 */
	@Override
	public void modifyMedicine(Medicine medicine) {
		try {
			
			System.out.println("修改");
			medicineMapper.updateMedi(medicine);
			
		} catch (Exception e) {
			
			System.out.println("modifyMedicine错误");
			e.printStackTrace();
			throw new MedicineServiceException("modifyMedicine错误");
			
		}
		
	}


	@Override
	public void delMedicineByMediID(Medicine medicine) {
		try {
			
			System.out.println("删除:"+medicine.getMediId());
			
			medicineMapper.deleteMedi(medicine);
			
		} catch (Exception e) {
			
			System.out.println("delMedicineByMediID错误");
			e.printStackTrace();
			throw new MedicineServiceException("delMedicineByMediID错误");
			
		}
	}

}
